package com.esun.payment_api.service.impl;

import com.esun.common.utils.PinyinUtils;
import com.esun.payment_api.domain.DkCusSubs;
import com.esun.payment_api.domain.User;
import com.esun.payment_api.dto.DkCusSubsQueryParam;
import com.esun.payment_api.mapper.DkCusSubsMapper;
import com.esun.payment_api.service.DkCusSubsService;
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.Collator;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DkCusSubsServiceImpl implements DkCusSubsService {

    @Resource
    private DkCusSubsMapper dkCusSubsMapper ;


    @Override
    public List<DkCusSubs> getList(Integer dept_id, String searchStr,Integer co_id, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<DkCusSubs> list = dkCusSubsMapper.getList(dept_id, searchStr, co_id);
        System.out.println(list.size());
        if (list != null && list.size() > 0) {
//            Comparator com = Collator.getInstance(Locale.ENGLISH);
            //System.out.println(com);
            //按字母排序
//           Collections.sort(list.stream().map(DkCusSubs::getName).collect(Collectors.toList()), com);
            //循环找出首字母一样的数据

            for (DkCusSubs e : list) {
                try {
                    String name = e.getName();
                    name = name.replaceAll("《", "<");
                    name = name.replaceAll("》", ">");
                    name = name.replaceAll("（", "(");
                    name = name.replaceAll("）", ")");
                    String zm = PinyinUtils.getFullSpell(name).substring(0, 1);
                    Character character = zm.charAt(0);
                    System.out.println(character);
                    if ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z')) {
                        Character firstCharacter = name.substring(0, 1).charAt(0);
                        if ((firstCharacter >= 'a' && firstCharacter <= 'z') || (firstCharacter >= 'A' && firstCharacter <= 'Z')) {
                            e.setInitials("#");
                        } else {
                            e.setInitials(zm);
                        }
                    } else {
                        e.setInitials("#");
                    }
                } catch (Exception exception) {
                    e.setInitials("#");
                }
            }
//            Collections.sort(list, new Comparator<DkCusSubs>() {
//                @Override
//                public int compare(DkCusSubs o1, DkCusSubs o2) {
//                    return o1.getInitials().charAt(0) - o2.getInitials().charAt(0);
//                }
//            });
        }
        return list;
    }

    @Override
    public Integer getSubscribeNumber(User user){
        List<DkCusSubs> list = dkCusSubsMapper.getList(user.getDeptId().intValue(), null, user.getCoId().intValue());
        return list.size();
    }

    @Override
    public void customerSubscribe(Integer cusId, User user) {
        dkCusSubsMapper.customerSubscribe(cusId, user.getDeptId(), user.getCoId(), user.getId());
    }

    @Override
    public void customerUnsubscribe(Integer cusId, User user) {
        dkCusSubsMapper.customerUnsubscribe(cusId, user.getDeptId(), user.getCoId(), user.getId());
    }
}
